/*
 * Copyright (C) 2004, Velocityme Partnership
 * ActionActions.java
 *
 * Created on 06 October 2004, 08:25
 */

package com.velocityme.www.interfaces.nodeactions;

import com.velocityme.entity.Action;
import com.velocityme.session.ActionLocal;
import com.velocityme.session.ContactableLocal;
import com.velocityme.session.KeyLocal;
import com.velocityme.session.NodeLocal;
import com.velocityme.www.actionforms.nodeactionforms.ActionActionForm;
import java.util.Calendar;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author  Robert
 */
public class ActionActions extends ActivityActions {

    /** Creates a new instance of ActionActions */
    public ActionActions() {
    }

    @Override
    public ActionForward show(ActionMapping mapping,
                              ActionForm form,
                              HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        Long nodeId = (Long)request.getAttribute("nodeId");
        HttpSession session = request.getSession();
        KeyLocal key = (KeyLocal)session.getAttribute("sessionKey");
        Action action = lookupActionBean().findAction(key, nodeId);
        session.setAttribute("action", action);
        session.setAttribute("actionHistory", lookupNodeBean().getHistory(key, action));
        return mapping.findForward("ACTION_DISPLAY");
    }

    @Override
    public ActionForward showCreate(ActionMapping mapping,
                                    ActionForm form,
                                    HttpServletRequest request,
                                    HttpServletResponse response) throws Exception {
        Long nodeId = (Long)request.getAttribute("nodeId");
        HttpSession session = request.getSession();
        KeyLocal key = (KeyLocal)session.getAttribute("sessionKey");
        configureCreate(session, key);
        ActionActionForm actionAF = new ActionActionForm();
        actionAF.setOwnerId(key.getPerson().getId());
        actionAF.setInterested(true);
        session.setAttribute("actionAF", actionAF);
        Action action = new Action();
        action.setParentNode(lookupNodeBean().findNode(key, nodeId));
        session.setAttribute("action", action);
        return mapping.findForward("ACTION_EDIT");
    }

    @Override
    public ActionForward showEdit(ActionMapping mapping,
                                  ActionForm form,
                                  HttpServletRequest request,
                                  HttpServletResponse response) throws Exception {
        Long nodeId = (Long)request.getAttribute("nodeId");
        HttpSession session = request.getSession();
        KeyLocal key = (KeyLocal)session.getAttribute("sessionKey");
        Action action = lookupActionBean().findAction(key, nodeId);
        session.setAttribute("action", action);
        configureEdit(session, key, action);
        ActionActionForm actionAF = new ActionActionForm();
        actionAF.setOwnerId(action.getOwner().getId());
        actionAF.setTaskTypeId(action.getTaskStateMachine().getId());
        actionAF.setPercentageComplete(action.getPercentageComplete());
        actionAF.setDurationTotalMins(action.getExpectedDuration_min());
        Calendar date = Calendar.getInstance();
        date.setTime(action.getStartDate());
        session.setAttribute("actionAF", actionAF);
        return mapping.findForward("ACTION_EDIT");
    }

    @Override
    public ActionForward edit(ActionMapping mapping,
                              ActionForm form,
                              HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        HttpSession session = request.getSession();
        KeyLocal key = (KeyLocal)session.getAttribute("sessionKey");
        Long nodeId;
        if (request.getParameter("btnSubmit").equals("OK")) {
            Action action = (Action)session.getAttribute("action");
            ((ActionActionForm)form).extractAction(key, action);
            if (action.getId() != null) {
                lookupActionBean().editAction(key, action);
            }
            else {
                action = lookupActionBean().createAction(key, action, ((ActionActionForm)form).isInterested());
            }
            nodeId = action.getId();
        }
        // Cancel simply displays the original node again
        else {
            nodeId = (Long)session.getAttribute("nodeId");
        }
        request.setAttribute("nodeId", nodeId);
        return mapping.findForward("ACTION_SUCCESS");
    }

    private ActionLocal lookupActionBean() {
        try {
            Context c = new InitialContext();
            return (ActionLocal) c.lookup("java:comp/env/ActionBean");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }

    private NodeLocal lookupNodeBean() {
        try {
            Context c = new InitialContext();
            return (NodeLocal) c.lookup("java:comp/env/NodeBean");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }

    private ContactableLocal lookupContactableBean() {
        try {
            Context c = new InitialContext();
            return (ContactableLocal) c.lookup("java:comp/env/ContactableBean");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }

}
